package cn.fl.capital.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapFundRemLogQueryCondition;
import cn.fl.capital.service.ICapFundRemLogService;
import cn.fl.capital.vo.CapFundRemLogVO;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;

/**
 * @Description: 客户资金备注记录 控制层
 */
@Controller
@RequestMapping("/capital/capFundRemLog")
public class CapFundRemLogController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(CapFundRemLogController.class);
	
	/**
	 * CapFundRemLog 服务层BEAN
	 */
	@Autowired
	private ICapFundRemLogService capFundRemLogService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toListPage", "加载CapFundRemLog列表页面.",CurrentThreadContext.getCurrentUserName());
		String id = request.getParameter("id");//资金id
		ModelAndView mav = new ModelAndView("capital/capFundRemLog/capFundRemLogList");
		mav.addObject("id",id);
		return mav;
	}
	
	/**
	 * 到新增页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toAddPage", "加载CapFundRemLog Add页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView mav = new ModelAndView("capital/capFundRemLog/capFundRemLogModify");
		//this.prepare(request,mav);
		return mav;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toModifyPage", "加载CapFundRemLog Modify页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView mav = new ModelAndView("capital/capFundRemLog/capFundRemLogModify");
		//this.prepare(request,mav);
		
		String id=request.getParameter("id");
		CapFundRemLogVO capFundRemLogVO = this.capFundRemLogService.selectById(Long.valueOf(id));
        mav.addObject("capFundRemLogVO", capFundRemLogVO);
		return mav;
	}
	
	//加载数据字典数据、传值等 ？
    public void prepare(HttpServletRequest request,ModelAndView mav){
    	
    }

	/**
	 * 初始化列表
	 */
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initCapFundRemLogList(HttpServletRequest request, HttpServletResponse response) {
		CapFundRemLogQueryCondition cfrl = new CapFundRemLogQueryCondition();
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		//资金id
		String fundId = request.getParameter("fundId").trim();
		if (StringUtils.isNotEmpty(fundId)) {
			//设置查询条件
			cfrl.setFundId(Long.parseLong(fundId));
		}else{
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("流程参数不完整！"+ this.getRequestParameter(request));
			return result;
		}
		BaseLogUtils.info(logger, "initCapFundRemLogList", "初始化CapFundRemLog列表页面数据.DATA:"+JSONObject.toJSONString(cfrl), CurrentThreadContext.getCurrentUserName());
	    List<CapFundRemLogVO> searchList = this.capFundRemLogService.searchList(cfrl);

	    if(CollectionUtils.isNotEmpty(searchList)){
	    	//不为空放入数据返回
	    	result.setSuccess(1);
	    	result.setData(searchList);
	    }else{
	    	result.setData(new  ArrayList<CapFundRemLogVO>());
	    }
		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeCapFundRemLogByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "removeCapFundRemLogByIds", "删除CapFundRemLog数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }
		
		try {
			boolean success = this.capFundRemLogService.removeByIds(idsList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removeCapFundRemLogByIds", "删除CapFundRemLog数据失败,ID:"+idsList.toString(), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removeCapFundRemLogByIds", "删除CapFundRemLog数据成功,ID:"+idsList.toString(), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "removeCapFundRemLogByIds", "删除CapFundRemLog数据失败,ID:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeCapFundRemLogByIds", "删除CapFundRemLog数据失败,系统错误.ID:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getCapFundRemLogById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "getCapFundRemLogById", "获取CapFundRemLog数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			CapFundRemLogVO vo = this.capFundRemLogService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getCapFundRemLogById", "获取CapFundRemLog数据不存在,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getCapFundRemLogById", "获取CapFundRemLog数据成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "getCapFundRemLogById", "获取CapFundRemLog数据失败,ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getCapFundRemLogById", "获取CapFundRemLog数据失败,系统错误.ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/addCapFundRemarksLog.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addCapFundRemLog(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		CapFundRemLogVO entity = null;//获取参数
		String id = request.getParameter("id").trim();//资金id
		String content = request.getParameter("content");//内容
		try {
			entity = new CapFundRemLogVO();
			entity.setFundId(Long.parseLong(id));//资金id
			entity.setRemark(content); //备注内容
			entity.setCrtTm(new Date());//创建时间
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());//创建人id
			entity.setIsDel(0);  //是否删除（1是0否）
			entity.setOptUsrId(CurrentThreadContext.getCurrentUserId());//当前用户id
			entity.setOptUsrNm(CurrentThreadContext.getCurrentUserName());//当前用户姓名
			
			Long isno = this.capFundRemLogService.add(entity);
			result.setData(isno);
			BaseLogUtils.info(logger, "addCapFundRemLog", "新增CapFundRemLog数据成功,ID:"+id+".DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.info(logger, "addCapFundRemLog", "新增CapFundRemLog数据失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.error(logger, "addCapFundRemLog", "新增CapFundRemLog数据失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}


	

}

